<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Formats the interval</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="dateinterval.createfromdatestring.html">DateInterval::createFromDateString</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="class.dateperiod.html">DatePeriod</a></div>
 <div class="up"><a href="class.dateinterval.html">DateInterval</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="dateinterval.format" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DateInterval::format</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0)</p><p class="refpurpose"><span class="refname">DateInterval::format</span> &mdash; <span class="dc-title">Formats the interval</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-dateinterval.format-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">string</span> <span class="methodname"><strong>DateInterval::format</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>
   )</div>

  <p class="para rdfs-comment">
   Formats the interval.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-dateinterval.format-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">format</code></em></dt>

     <dd>

      <p class="para">
       <table class="doctable table">
        <caption><strong>
         The following characters are recognized in the
         <em><code class="parameter">format</code></em> parameter string.
         Each format character must be prefixed by a percent sign
         (<em>%</em>).
        </strong></caption>
        
         <thead>
          <tr>
           <th><em><code class="parameter">format</code></em> character</th>
           <th>Description</th>
           <th>Example values</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td><em>%</em></td>
           <td>Literal <em>%</em></td>
           <td><em>%</em></td>
          </tr>

          <tr>
           <td><em>Y</em></td>
           <td>Years, numeric, at least 2 digits with leading 0</td>
           <td><em>01</em>, <em>03</em></td>
          </tr>

          <tr>
           <td><em>y</em></td>
           <td>Years, numeric</td>
           <td><em>1</em>, <em>3</em></td>
          </tr>

          <tr>
           <td><em>M</em></td>
           <td>Months, numeric, at least 2 digits with leading 0</td>
           <td><em>01</em>, <em>03</em>, <em>12</em></td>
          </tr>

          <tr>
           <td><em>m</em></td>
           <td>Months, numeric</td>
           <td><em>1</em>, <em>3</em>, <em>12</em></td>
          </tr>

          <tr>
           <td><em>D</em></td>
           <td>Days, numeric, at least 2 digits with leading 0</td>
           <td><em>01</em>, <em>03</em>, <em>31</em></td>
          </tr>

          <tr>
           <td><em>d</em></td>
           <td>Days, numeric</td>
           <td><em>1</em>, <em>3</em>, <em>31</em></td>
          </tr>

          <tr>
           <td><em>a</em></td>
           <td>Total number of days as a result of a <span class="methodname"><a href="datetime.diff.html" class="methodname">DateTime::diff()</a></span> or <em>(unknown)</em> otherwise</td>
           <td><em>4</em>, <em>18</em>, <em>8123</em></td>
          </tr>

          <tr>
           <td><em>H</em></td>
           <td>Hours, numeric, at least 2 digits with leading 0</td>
           <td><em>01</em>, <em>03</em>, <em>23</em></td>
          </tr>

          <tr>
           <td><em>h</em></td>
           <td>Hours, numeric</td>
           <td><em>1</em>, <em>3</em>, <em>23</em></td>
          </tr>

          <tr>
           <td><em>I</em></td>
           <td>Minutes, numeric, at least 2 digits with leading 0</td>
           <td><em>01</em>, <em>03</em>, <em>59</em></td>
          </tr>

          <tr>
           <td><em>i</em></td>
           <td>Minutes, numeric</td>
           <td><em>1</em>, <em>3</em>, <em>59</em></td>
          </tr>

          <tr>
           <td><em>S</em></td>
           <td>Seconds, numeric, at least 2 digits with leading 0</td>
           <td><em>01</em>, <em>03</em>, <em>57</em></td>
          </tr>

          <tr>
           <td><em>s</em></td>
           <td>Seconds, numeric</td>
           <td><em>1</em>, <em>3</em>, <em>57</em></td>
          </tr>

          <tr>
           <td><em>R</em></td>
           <td>Sign &quot;<em>-</em>&quot; when negative, &quot;<em>+</em>&quot; when positive</td>
           <td><em>-</em>, <em>+</em></td>
          </tr>

          <tr>
           <td><em>r</em></td>
           <td>Sign &quot;<em>-</em>&quot; when negative, empty when positive</td>
           <td><em>-</em>, <em></td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-dateinterval.format-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the formatted interval.
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-dateinterval.format-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    The <span class="methodname"><strong>DateInterval::format()</strong></span> method does not 
    recalculate carry over points in time strings nor in date segments. This 
    is expected because it is not possible to overflow values like <em>&quot;32 days&quot;</em>
    which could be interpreted as anything from <em>&quot;1 month and 4 days&quot;</em> 
    to <em>&quot;1 month and 1 day&quot;</em>.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 examples" id="refsect1-dateinterval.format-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2221">
    <p><strong>Example #1 <a href="class.dateinterval.html" class="classname">DateInterval</a> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$interval&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P2Y4DT6H8M'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$interval</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%d&nbsp;days'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
4 days
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2222">
    <p><strong>Example #2 <a href="class.dateinterval.html" class="classname">DateInterval</a> and carry over points</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$interval&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P32D'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$interval</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%d&nbsp;days'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
32 days
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2223">
    <p><strong>Example #3 
     <a href="class.dateinterval.html" class="classname">DateInterval</a> and 
     <span class="methodname"><a href="datetime.diff.html" class="methodname">DateTime::diff()</a></span> with the %a and %d modifiers
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$january&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2010-01-01'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$february&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2010-02-01'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$interval&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$february</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">diff</span><span style="color: #007700">(</span><span style="color: #0000BB">$january</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;%a&nbsp;will&nbsp;output&nbsp;the&nbsp;total&nbsp;number&nbsp;of&nbsp;days.<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$interval</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%a&nbsp;total&nbsp;days'</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;While&nbsp;%d&nbsp;will&nbsp;only&nbsp;output&nbsp;the&nbsp;number&nbsp;of&nbsp;days&nbsp;not&nbsp;already&nbsp;covered&nbsp;by&nbsp;the<br />//&nbsp;month.<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$interval</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%m&nbsp;month,&nbsp;%d&nbsp;days'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
31 total days
1 month, 0 days
</pre></div>
    </div>
   </div>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="dateinterval.createfromdatestring.html">DateInterval::createFromDateString</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="class.dateperiod.html">DatePeriod</a></div>
 <div class="up"><a href="class.dateinterval.html">DateInterval</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
